/*
 * Copyright (C) 2022 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xjh.him.adapter.entity;

import android.annotation.SuppressLint;

import com.xjh.him.dto.friend.FriendDTO;
import com.xjh.him.dto.friendgroupmsg.FriendGroupMsgDTO;
import com.xjh.him.dto.user.UserDTO;
import com.xjh.him.dto.usergroupmsg.UserGroupMsgDTO;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Optional;

public class ChatMessage implements Serializable {
    public static final String DIRECTION_LEFT = "left";
    public static final String DIRECTION_RIGHT = "right";

    public static final String TYPE_TEXT = "text";
    public static final String TYPE_IMAGE = "image";
    public static final String TYPE_AUDIO = "audio";
    public static final String TYPE_USER_INVITE = "inviteUser";

    public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("MM月dd日 HH:mm");
    public static final String INVITE_TIP_FORMAT = "%s邀请%s加入了群聊";

    private String msgType;
    private String itemDirection;
    private Long senderId;
    private String senderName;
    private String senderAvatarUrl;
    private String textMsg;
    private String fileUrl;
    private Date createTime;

    private String inviteTip;

    public ChatMessage() {}

    public ChatMessage(String itemDirection, String msgType, String senderName, String textMsg, Date createTime) {
        this.itemDirection = itemDirection;
        this.msgType = msgType;
        this.senderName = senderName;
        this.textMsg = textMsg;
        this.createTime = createTime;
    }

    public static ChatMessage convertFrom(UserDTO userDTO, FriendDTO friendDTO, FriendGroupMsgDTO msgDTO) {
        ChatMessage chatMessage = new ChatMessage();
        if (userDTO.getId().equals(msgDTO.getSenderId())) {
            chatMessage.setItemDirection(DIRECTION_RIGHT);
            chatMessage.setSenderId(userDTO.getId());
            chatMessage.setSenderName(userDTO.getNickname());
            chatMessage.setSenderAvatarUrl(userDTO.getAvatarUrl());
        }else {
            chatMessage.setItemDirection(DIRECTION_LEFT);
            chatMessage.setSenderId(friendDTO.getId());
            chatMessage.setSenderName(friendDTO.getNickname());
            chatMessage.setSenderAvatarUrl(friendDTO.getAvatarUrl());
        }
        chatMessage.setMsgType(msgDTO.getMsgType());
        if (TYPE_TEXT.equals(msgDTO.getMsgType())) {
            chatMessage.setTextMsg(msgDTO.getMsgContent());
        }else if (TYPE_IMAGE.equals(msgDTO.getMsgType())) {
            chatMessage.setFileUrl(msgDTO.getMsgContentUrl());
        }
        chatMessage.setCreateTime(msgDTO.getCreateTime());
        return chatMessage;
    }

    @SuppressLint("NewApi")
    public static ChatMessage convertFrom(UserDTO userDTO, List<SessionMember> memberList, UserGroupMsgDTO msgDTO) {
        ChatMessage chatMessage = new ChatMessage();
        if (userDTO.getId().equals(msgDTO.getUserId())) {
            chatMessage.setItemDirection(DIRECTION_RIGHT);
            chatMessage.setSenderId(userDTO.getId());
            chatMessage.setSenderName(userDTO.getNickname());
            chatMessage.setSenderAvatarUrl(userDTO.getAvatarUrl());
        }else {
            chatMessage.setItemDirection(DIRECTION_LEFT);
            Optional<SessionMember> optional = memberList.stream()
                    .filter(item -> item.getUserId().equals(msgDTO.getUserId()))
                    .findFirst();
            if (optional.isPresent()) {
                SessionMember sessionMember = optional.get();
                chatMessage.setSenderId(sessionMember.getUserId());
                chatMessage.setSenderName(sessionMember.getUserNickname());
                chatMessage.setSenderAvatarUrl(sessionMember.getUserAvatar());
            }
        }
        chatMessage.setMsgType(msgDTO.getMsgType());
        if (TYPE_TEXT.equals(msgDTO.getMsgType())) {
            chatMessage.setTextMsg(msgDTO.getMsgContent());
        }else if (TYPE_IMAGE.equals(msgDTO.getMsgType())) {
            chatMessage.setFileUrl(msgDTO.getMsgContentUrl());
        }
        chatMessage.setCreateTime(msgDTO.getCreateTime());
        return chatMessage;
    }

    public static ChatMessage convertFromUser(UserDTO userDTO, String msgType, String textMsg, String fileUrl) {
        ChatMessage chatMessage = new ChatMessage();
        chatMessage.setItemDirection(DIRECTION_RIGHT);
        chatMessage.setSenderId(userDTO.getId());
        chatMessage.setSenderName(userDTO.getNickname());
        chatMessage.setSenderAvatarUrl(userDTO.getAvatarUrl());
        chatMessage.setMsgType(msgType);
        if (TYPE_TEXT.equals(msgType)) {
            chatMessage.setTextMsg(textMsg);
        }else if (TYPE_IMAGE.equals(msgType)) {
            chatMessage.setFileUrl(fileUrl);
        }
        chatMessage.setCreateTime(new Date());
        return chatMessage;
    }

    public static FriendGroupMsgDTO convertTo(Long groupId, Long friendId, ChatMessage chatMessage) {
        FriendGroupMsgDTO msgDTO = new FriendGroupMsgDTO();
        msgDTO.setFriendGroupId(groupId);
        msgDTO.setSenderId(chatMessage.getSenderId());
        msgDTO.setReceiverId(friendId);
        msgDTO.setMsgType(chatMessage.getMsgType());
        if (TYPE_TEXT.equals(chatMessage.getMsgType())) {
            msgDTO.setMsgContent(chatMessage.getTextMsg());
        }else if (TYPE_IMAGE.equals(chatMessage.getMsgType())) {
            msgDTO.setMsgContentUrl(chatMessage.getFileUrl());
        }
        msgDTO.setCreateTime(chatMessage.getCreateTime());
        return msgDTO;
    }

    public static UserGroupMsgDTO convertTo(Long groupId, ChatMessage chatMessage) {
        UserGroupMsgDTO msgDTO = new UserGroupMsgDTO();
        msgDTO.setUserGroupId(groupId);
        msgDTO.setUserId(chatMessage.getSenderId());
        msgDTO.setUserNickname(chatMessage.getSenderName());
        msgDTO.setMsgType(chatMessage.getMsgType());
        if (TYPE_TEXT.equals(chatMessage.getMsgType())) {
            msgDTO.setMsgContent(chatMessage.getTextMsg());
        }else if (TYPE_IMAGE.equals(chatMessage.getMsgType())) {
            msgDTO.setMsgContentUrl(chatMessage.getFileUrl());
        }
        msgDTO.setCreateTime(chatMessage.getCreateTime());
        return msgDTO;
    }

    public static String getFormatInviteTip(String memberName, String invitedUserName) {
        return String.format(INVITE_TIP_FORMAT, memberName, invitedUserName);
    }

    public String getItemDirection() {
        return itemDirection;
    }

    public void setItemDirection(String itemDirection) {
        this.itemDirection = itemDirection;
    }

    public Long getSenderId() {
        return senderId;
    }

    public void setSenderId(Long senderId) {
        this.senderId = senderId;
    }

    public String getSenderName() {
        return senderName;
    }

    public void setSenderName(String senderName) {
        this.senderName = senderName;
    }

    public String getSenderAvatarUrl() {
        return senderAvatarUrl;
    }

    public void setSenderAvatarUrl(String senderAvatarUrl) {
        this.senderAvatarUrl = senderAvatarUrl;
    }

    public String getTextMsg() {
        return textMsg;
    }

    public void setTextMsg(String textMsg) {
        this.textMsg = textMsg;
    }

    public String getFileUrl() {
        return fileUrl;
    }

    public void setFileUrl(String fileUrl) {
        this.fileUrl = fileUrl;
    }

    public String getMsgType() {
        return msgType;
    }

    public void setMsgType(String msgType) {
        this.msgType = msgType;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getInviteTip() {
        return inviteTip;
    }

    public void setInviteTip(String inviteTip) {
        this.inviteTip = inviteTip;
    }
}
